package com.javaclimb.filmbuy.controller;

import com.github.pagehelper.PageInfo;
import com.javaclimb.filmbuy.common.Result;
import com.javaclimb.filmbuy.common.ResultCode;
import com.javaclimb.filmbuy.entity.GoodsInfo;
import com.javaclimb.filmbuy.entity.OrderInfo;
import com.javaclimb.filmbuy.entity.UserInfo;
import com.javaclimb.filmbuy.exception.CustomException;
import com.javaclimb.filmbuy.service.GoodsInfoService;
import com.javaclimb.filmbuy.service.OrderInfoService;
import com.javaclimb.filmbuy.service.UserInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/*
* 订单信息的controller层
* */
@RestController
@RequestMapping(value = "/orderInfo")
public class OrderInfoController {
    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private GoodsInfoService goodsInfoService;

    /*
    * 增加一个订单信息
    * */
    @PostMapping
    public Result<OrderInfo> add(@RequestBody OrderInfo orderInfo){
        Long userId = orderInfo.getUserId();
        //用户并没有登录
        if (userId == null){
            throw new CustomException((ResultCode.PARAM_ERROR));
        }

        //用户找了个不存在的电影
        GoodsInfo goodsInfo = goodsInfoService.findById(orderInfo.getGoodsId());
        if (goodsInfo == null){
            throw new CustomException((ResultCode.PARAM_ERROR));
        }
        orderInfoService.add(orderInfo,goodsInfo);
        return Result.success(orderInfo);
    }

    /*
    * 根据主键id删除一个订单信息
    * */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        orderInfoService.delete(id);
        return Result.success();
    }

    /*
    * 更新一个订单信息
    * */
    @PutMapping
    public Result<OrderInfo> update(@RequestBody OrderInfo orderInfo) {
        orderInfoService.update(orderInfo);
        return Result.success(orderInfo);
    }

    /*
     * 根据主键id查询一个订单信息
     * */
    @GetMapping("/{id}")
    public Result<OrderInfo> findById(@PathVariable Long id){
        OrderInfo orderInfo = orderInfoService.findById(id);
        return Result.success(orderInfo);
    }

    /**
     * 前端页面的订单列表
     */
    @GetMapping("/page/front")
    public Result<PageInfo<OrderInfo>> findFrontPage(
            @RequestParam(required = true) Long userId,
            @RequestParam(required = true) Integer level,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(orderInfoService.findFrontPages(userId, level, pageNum, pageSize));
    }
    /**
     * 后端页面的订单列表
     */
    @GetMapping("/page")
    public Result<PageInfo<OrderInfo>> findPage(
            @RequestParam(required = true) Long userId,
            @RequestParam(required = true) Integer level,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            HttpServletRequest request){
        return Result.success(orderInfoService.findPages(userId, level, pageNum, pageSize,request));
    }
    /**
     * 改变订单状态
     */
   @PostMapping("/status{id}/{status}")
    public Result status(@PathVariable Long id,String status){
       orderInfoService.changeStatus(id,status);
       return  Result.success();
   }
}
